package com.canxi.earn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.canxi.common.core.page.TableDataInfo;
import com.canxi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.canxi.earn.domain.bo.CxBannerBo;
import com.canxi.earn.domain.vo.CxBannerVo;
import com.canxi.earn.domain.CxBanner;
import com.canxi.earn.mapper.CxBannerMapper;
import com.canxi.earn.service.ICxBannerService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import com.canxi.cxcommon.utils.PageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.hutool.core.util.StrUtil;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;

/**
 * 轮播图管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-04
 */
@RequiredArgsConstructor
@Service
public class CxBannerServiceImpl  extends JoinServiceImpl<CxBannerMapper, CxBanner>  implements ICxBannerService {

    private final CxBannerMapper baseMapper;

    @Override
    public TableDataInfo<CxBannerVo> queryCxBannerList(CxBannerBo cxBannerBo) {

        return PageUtils.buildDataInfo(this.baseMapper.queryCxBannerList(PageUtils.buildPage(), BeanUtil.toBean(cxBannerBo, CxBannerVo.class)));
    }

    @Override
    public List<CxBannerVo> queryCxBannerAll(CxBannerBo cxBannerBo) {
        return this.baseMapper.queryCxBannerList(BeanUtil.toBean(cxBannerBo, CxBannerVo.class));
    }



    /**
     * 查询轮播图管理
     */
    @Override
    public CxBannerVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询轮播图管理列表
     */
    @Override
    public TableDataInfo<CxBannerVo> queryPageList(CxBannerBo bo, PageQuery pageQuery) {
        JoinLambdaWrapper<CxBanner> lqw = buildQueryWrapper(bo);
        Page<CxBannerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询轮播图管理列表
     */
    @Override
    public List<CxBannerVo> queryList(CxBannerBo bo) {
        JoinLambdaWrapper<CxBanner> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private JoinLambdaWrapper<CxBanner> buildQueryWrapper(CxBannerBo bo) {
        Map<String, Object> params = bo.getParams();
        JoinLambdaWrapper<CxBanner> lqw = joinLambdaQueryWrapper();
        lqw.eq(StrUtil.isNotBlank(bo.getOssIds()), CxBanner::getOssIds, bo.getOssIds());
        lqw.eq(StrUtil.isNotBlank(bo.getIconUrl()), CxBanner::getIconUrl, bo.getIconUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getToType()), CxBanner::getToType, bo.getToType());
        lqw.eq(StrUtil.isNotBlank(bo.getToUrl()), CxBanner::getToUrl, bo.getToUrl());
        lqw.eq(StrUtil.isNotBlank(bo.getShowType()), CxBanner::getShowType, bo.getShowType());
        lqw.eq(StrUtil.isNotBlank(bo.getBannerStatus()), CxBanner::getBannerStatus, bo.getBannerStatus());
        return lqw;
    }

    /**
     * 新增轮播图管理
     */
    @Override
    public Boolean insertByBo(CxBannerBo bo) {
        CxBanner add = BeanUtil.toBean(bo, CxBanner.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改轮播图管理
     */
    @Override
    public Boolean updateByBo(CxBannerBo bo) {
        CxBanner update = BeanUtil.toBean(bo, CxBanner.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CxBanner entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除轮播图管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
